<?php
/**
 * Created by PhpStorm.
 * User: dingjinlai
 * Date: 2019/2/10
 * Time: 下午1:25
 */

class shelves_model extends CI_Model
{

    public function get_shelves_id($sl_title) {
        $query = $this->db->from('shelves')->where("sl_title = '{$sl_title}'")->get();

        $shelves = $query->row_array();

        return $shelves['sl_id'];
    }

    public function shelves_list($param = array(), $is_all = 0) {

        $this->db->from('shelves')
            ->where('is_del = 0');

        if ($is_all == 0) {
            $this->db->limit($this->limit_rows,$this->limit_rows * $param['page']);
        }

        $query = $this->db->get();

        return $query->result_array();
    }

    public function shelves_insert($data = array()) {

        $shelves_data = array(
            'sl_title' => $data['sl_title'],
            'list_shelves' => $data['list_shelves'],
            'level_shelves' => $data['level_shelves'],
            'loca_shelves' => $data['loca_shelves'],
            'is_del' => 0,
            'add_time' => time()
        );

        $this->db->insert('shelves', $shelves_data);
    }

    public function shelves_update($data = array(), $sl_id)
    {
        $this->db->update('shelves', $data, array('sl_id' => $sl_id));
    }

    public function shelves_sku_info($param = array())
    {
        $query = $this->db->from('shelves_sku')->where($param)->get();
        $info = $query->row_array();

        return $info;
    }

    public function shelves_info($param = array())
    {
        $query = $this->db->from('shelves')->where($param)->get();

        $info = $query->row_array();

        return $info;
    }

    public function shelves_del($sl_id)
    {
        $this->db->update('shelves', array(
            'is_del' => 1
        ), array('sl_id' => $sl_id));

        $this->db->update('shelves_sku', array(
            'is_del' => 1
        ), array('sl_id' => $sl_id));
    }

    public function set_shelves_sku($data = array())
    {

        $shelves_sku_data = array(
            'sku_id' => $data['sku_id'],
            'sl_id' => $data['sl_id'],
            'sl_title' => $data['sl_title'],
            'is_del' => 0,
            'add_time' => time()
        );

        $shelves_sku_info = $this->shelves_sku_info(array(
            'sku_id' => $data['sku_id'],
            'sl_id' => $data['sl_id']
        ));

        if ($shelves_sku_info) {
            if ($shelves_sku_info['is_del'] == 1) {
                $this->db->update('shelves_sku', array(
                    'is_del' => 1
                ), "sls_id = '{$shelves_sku_info['sls_id']}'");
            }
        } else {
            $this->db->insert('shelves_sku', $shelves_sku_data);
        }

    }
}